<template>
  <div>
    <div class="app-container">
      <!--表单-->
      <el-form :inline="true" class="demo-form-inline"> 
        <el-form-item>
          <el-select v-model="searchObj.area" clearable placeholder="请选择地块">
            <el-option label="一号地" value="1"/>
            <el-option label="二号地" value="2"/>
            <el-option label="三号地" value="3"/>
          </el-select>
      </el-form-item>
<!-- temperature  humidity     dan     lin     jia -->
        <el-form-item>
          <el-select v-model="searchObj.type" clearable placeholder="请选择参数">
            <el-option label="温度" value="temperature"/>
            <el-option label="湿度" value="humidity"/>
            <el-option label="氮" value="dan"/>
            <el-option label="磷" value="lin"/>
            <el-option label="钾" value="jia"/>
          </el-select>
      </el-form-item>

      <el-form-item>
        <el-date-picker
        v-model="searchObj.begin"
        type="date"
        placeholder="选择开始日期"
        value-format="yyyy-MM-dd" />
      </el-form-item>

      <el-form-item>
        <el-date-picker
        v-model="searchObj.end"
        type="date"
        placeholder="选择截止日期"
        value-format="yyyy-MM-dd" />
      </el-form-item>

      <el-button
      :disabled="btnDisabled"
      type="primary"
      icon="el-icon-search"
      @click="showChart()">查询</el-button>
      
      </el-form>

      <div class="chart-container">
      <div id="chart" class="chart" style="height:500px;width:100%" />
    </div>
    </div>

    

    <div class="mod-demo-echarts">
      <el-alert
        title="提示："
        type="warning"
        :closable="false">
      </el-alert>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-card>
            <div id="J_chartLineBox" class="chart-box"></div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
  import echarts from 'echarts'
  export default {
    data () {
      return {
        chartLine: null,
        searchObj:{},
        searchObj: {
          area: '',
          type: '',
          begin: '',
          end: ''
        },
        btnDisabled:false,
        xData:[],
        yData:[]

      }
    },
    created() {

    },
    mounted () {
      this.initChartLine()
    },
    activated () {
      // 由于给echart添加了resize事件, 在组件激活时需要重新resize绘画一次, 否则出现空白bug
      if (this.chartLine) {
        this.chartLine.resize()
      }
    },
    methods: {
      showChart(searchObj){

        this.$http({
        url: this.$http.adornUrl(`/socket/daily/showData/${searchObj.area}/${searchObj.type}/${searchObj.begin}/${searchObj.end}`),
        method: 'get',
        }).then(({data}) => {
            this.xData = data.data.date_calculatedList
            this.yData = data.data.numDataList

            //调用真正生成图表的方法，改变值
            this.setChart()
        })
          
          
          
          
          // .then(response => {



          //   this.xData = response.data.date_calculatedList
          //   this.yData = response.data.numDataList

          //   //调用真正生成图表的方法，改变值
          //   this.setChart()
          // });


      },
      setChart(){


        // 基于准备好的dom，初始化echarts实例
        this.chart = echarts.init(document.getElementById('chart'))
        // console.log(this.chart)

        // 指定图表的配置项和数据
        var option = {
          // x轴是类目轴（离散数据）,必须通过data设置类目数据
          xAxis: {
            type: 'category',
            data: this.xData
          },
          // y轴是数据轴（连续数据）
          yAxis: {
            type: 'value'
          },
          // 系列列表。每个系列通过 type 决定自己的图表类型
          series: [{
            // 系列中的数据内容数组
            data: this.yData,
            // 折线图
            type: 'line'
          }]
        }
        this.chart.setOption(option)
      },

      // showChart(){
      //   this.chartLine = echarts.init(document.getElementById('J_chartLineBox'))
      // },
      // 折线图

      initChartLine () {
        var option = {
          'title': {
            'text': '折线图堆叠'
          },
          'tooltip': {
            'trigger': 'axis'
          },
          'legend': {
            'data': [ '邮件营销', '联盟广告', '视频广告', '直接访问', '搜索引擎' ]
          },
          'grid': {
            'left': '3%',
            'right': '4%',
            'bottom': '3%',
            'containLabel': true
          },
          'toolbox': {
            'feature': {
              'saveAsImage': { }
            }
          },
          'xAxis': {
            'type': 'category',
            'boundaryGap': false,
            'data': [ '周一', '周二', '周三', '周四', '周五', '周六', '周日' ]
          },
          'yAxis': {
            'type': 'value'
          },
          'series': [
            {
              'name': '邮件营销',
              'type': 'line',
              'stack': '总量',
              'data': [ 120, 132, 101, 134, 90, 230, 210 ]
            },
            {
              'name': '联盟广告',
              'type': 'line',
              'stack': '总量',
              'data': [ 220, 182, 191, 234, 290, 330, 310 ]
            },
            {
              'name': '视频广告',
              'type': 'line',
              'stack': '总量',
              'data': [ 150, 232, 201, 154, 190, 330, 410 ]
            },
            {
              'name': '直接访问',
              'type': 'line',
              'stack': '总量',
              'data': [ 320, 332, 301, 334, 390, 330, 320 ]
            },
            {
              'name': '搜索引擎',
              'type': 'line',
              'stack': '总量',
              'data': [ 820, 932, 901, 934, 1290, 1330, 1320 ]
            }
          ]
        }
        this.chartLine = echarts.init(document.getElementById('J_chartLineBox'))
        this.chartLine.setOption(option)
        window.addEventListener('resize', () => {
          this.chartLine.resize()
        })
      }
    }

  }
</script>

<style lang="scss">
  .mod-demo-echarts {
    > .el-alert {
      margin-bottom: 10px;
    }
    > .el-row {
      margin-top: -10px;
      margin-bottom: -10px;
      .el-col {
        padding-top: 10px;
        padding-bottom: 10px;
      }
    }
    .chart-box {
      min-height: 400px;
    }
  }
</style>
